package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.model.bo.CommentBO;
import com.cskaoyan.model.bo.ReplyBO;
import com.cskaoyan.model.vo.CommentVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
public class CommentServiceImpl implements  CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Override
    public BaseListData list(Integer page, Integer limit, String sort, String order) throws JsonProcessingException {
        PageHelper.startPage(page,limit);
        CommentExample commentExample = new CommentExample();
        commentExample.setOrderByClause(sort+" "+order);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        List<CommentVO> commentVOS = new ArrayList<>();
        for(int i=0;i<comments.size();i++)
        {   CommentVO commentVO = new CommentVO();
            commentVO.setId(comments.get(i).getId());
            commentVO.setValueId(comments.get(i).getValueId());
            commentVO.setContent(comments.get(i).getContent());
            commentVO.setUserId(comments.get(i).getUserId());
            commentVO.setType(comments.get(i).getType());
            commentVO.setHasPicture(comments.get(i).getHasPicture());
            ObjectMapper objectMapper = new ObjectMapper();
            String content = comments.get(i).getPicUrls();
            List<String> list = objectMapper.readValue(content,List.class);
            commentVO.setPicUrls(list);
            commentVO.setStar(comments.get(i).getStar());
            commentVO.setAddTime(comments.get(i).getAddTime());
            commentVO.setUpdateTime(comments.get(i).getUpdateTime());
            commentVO.setDeleted(comments.get(i).getDeleted());
            commentVOS.add(commentVO);
        }
        PageInfo<Comment> pageInfo = new PageInfo<Comment>(comments);
        long total = pageInfo.getTotal();
        return BaseListData.ok(commentVOS,total);
    }

    @Override
    public int reply(ReplyBO replyBO) {
        //没找到表
        return 1;
    }


    @Override
    public int delete(CommentBO commentBO) {
        int res = commentMapper.deleteByPrimaryKey(commentBO.getId());
        if(res == 1){
            return 1;
        }else{
            return 0;
        }
    }
}
